<template>
  <div class="todo-container">
    <div class="todo-wrap">
      <TodoHeader :addTodo="addTodo"/>
      <TodoList :todos="todos" :addTodo="addTodo" :deleteTodo="deleteTodo"/>
      <TodoFooter :todos="todos" :deleteCompletedTodos="deleteCompletedTodos" :selectAllTodos="selectAllTodos"/>
    </div>
  </div>
</template>

<script>
import TodoHeader from "./components/TodoHeader";
import TodoList from "./components/TodoList";
import TodoFooter from "./components/TodoFooter";

export default {
  name: "App",
  data() {
    return {
      todos: JSON.parse(window.localStorage.getItem('todos_key') || '[]')
    }
  },
  components: {
    TodoHeader, TodoList, TodoFooter
  },
  methods: {
    addTodo(newTodo) {
      this.todos.unshift(newTodo)
    },
    deleteTodo(index) {
      this.todos.splice(index, 1)
    },
    deleteCompletedTodos() {
      this.todos = this.todos.filter(e => !e.completed)
    },
    selectAllTodos(check) {
      this.todos.forEach(e => e.completed = check)
    }
  },
  watch: {
    todos: {
      deep: true,
      handler: function (value) {
        window.localStorage.setItem('todos_key', JSON.stringify(value))
      }
    }
  }
}
</script>

<style scoped>
/*app*/
.todo-container {
  width: 600px;
  margin: 0 auto;
}

.todo-container .todo-wrap {
  padding: 10px;
  border: 1px solid #ddd;
  border-radius: 5px;
}

</style>
